<?php

namespace app\controller\admin\cnf;

use app\bll\UserField as BllUserField;
use support\View;
use app\bll\ComponentElement as BllComponentElement;
use app\bll\UserTable as BllUserTable;
use Exception;
use app\bll\Datadict as BllDatadict;
use core\controller\AdminBase;

class UserModel extends AdminBase
{

    public function registerTable()
    {
        $bllUserField = new BllUserField();
        $fields       = $bllUserField->select('frame_user');

        //获取表单元素
        $bllComponentElement = new BllComponentElement();
        $elements = $bllComponentElement->select();

        $bllDatadict = new BllDatadict();
        $dicts = $bllDatadict->select();

        View::assign('dicts', array_column($dicts, null, 'id'));
        View::assign('fields', $fields);
        View::assign('elements', array_column($elements, null, 'ident'));
        return $this->view();
    }


    /**
     * 管理注册字段
     *
     * @return void
     */
    public function regFieldManage()
    {
        if (is_post()) {
            $bllUserField = new BllUserField();
            try {
                $bllUserField->batchsave(
                    array_values(request()->post('field')),
                    'frame_user'
                );
            } catch (Exception $ex) {
                return $this->error($ex);
            }
            return $this->success();
        } else {

            $bllUserField = new BllUserField();
            $fields       = $bllUserField->select('frame_user');

            //获取表单元素
            $bllComponentElement = new BllComponentElement();
            $elements = $bllComponentElement->select();

            //数据字典
            $bllDatadict = new BllDatadict();
            $dicts = $bllDatadict->select();

            View::assign('dicts', array_column($dicts, null, 'id'));
            View::assign('elements', array_column($elements, null, 'ident'));
            View::assign('fields', $fields);
            return $this->view(frame: false);
        }
    }

    public function infoTable()
    {
        $bllUserTable = new BllUserTable();
        $tables = $bllUserTable->selectInfoTable();
        View::assign('tables', $tables);
        return $this->view();
    }


    /**
     * 
     * 添加信息表
     * @return mixed
     */
    public function addInfoTable()
    {
        if (is_post()) {
            try {
                $bllUserTable = new BllUserTable();
                $bllUserTable->add(request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view();
        }
    }

    /**
     * 信息字段管理
     *
     * @return mixed
     */
    public function infoFieldManage()
    {
        $tableid      = request()->get('tableid');
        $bllUserTable = new BllUserTable();
        $tableinfo    = $bllUserTable->find($tableid);

        if (is_post()) {
            $bllUserField = new BllUserField();
            try {
                $bllUserField->batchsave(
                    array_values(request()->post('field')),
                    $tableinfo['name']
                );
            } catch (Exception $ex) {
                return $this->error($ex);
            }
            return $this->success();
        } else {

            $bllUserField = new BllUserField();
            $fields       = $bllUserField->select($tableinfo['name']);

            //获取表单元素
            $bllComponentElement = new BllComponentElement();
            $elements = $bllComponentElement->select();

            //数据字典
            $bllDatadict = new BllDatadict();
            $dicts = $bllDatadict->select();

            View::assign('dicts', array_column($dicts, null, 'id'));
            View::assign('elements', array_column($elements, null, 'ident'));
            View::assign('fields', $fields);
            return $this->view(frame: false);
        }
    }

    public function editInfoTable()
    {
        $tableid      = request()->get('tableid');
        $bllUserTable = new BllUserTable();
        if (request()->isPost()) {
            try {
                $bllUserTable->edit($tableid, request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {


            $tableinfo    = $bllUserTable->find($tableid);

            View::assign('tableinfo', $tableinfo);
            return $this->view();
        }
    }

    public  function delInfoTable()
    {
        $tableid      = request()->get('tableid');
        $bllUserTable = new BllUserTable();
        try {
            $bllUserTable->delete($tableid);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
